//||=========================================================================||
//||                                                                         ||
//||                           MandelbrotPicture.h                           ||
//||                                                                         ||
//||                           by the_best_goose))                           ||
//||                                                                         ||
//||=========================================================================||

#ifndef MANDELBROTPICTURE_H_
#define MANDELBROTPICTURE_H_

#include <QtOpenGL/QtOpenGL>
#include <QtGui/QtGui>
#include <QtCore/QtCore>
#include "MainWindow.h"
#include "TemplateMakers.h"

//-----------------------------------------------------------------------------

class MandelbrotPicture : public QGLWidget
{
	Q_OBJECT
    typedef QVector <IMandelbrotCounter*> CountersType;

public:

    MandelbrotPicture(CounterMaker pcounter, const MainWindow* pwind,
   	                  QWidget* pwgt = NULL);
    MandelbrotPicture(QSize size, CounterMaker pcounter,
    		          const MainWindow* pwind, QWidget* pwgt = NULL);

    virtual ~MandelbrotPicture();

    QPointF GetCoord(QPoint coord);
    QSize GetSize();

    void SetSize(QSize size);

    void keyReleaseEvent(QKeyEvent* pevent);

protected:

    void initializeGL();
    void paintGL();
    void mousePressEvent(QMouseEvent* pevent);
    void mouseReleaseEvent(QMouseEvent* pevent);
    void mouseDoubleClickEvent(QMouseEvent* pevent);
    void mouseMoveEvent(QMouseEvent* pevent);
    void wheelEvent(QWheelEvent* pevent);

signals:

    void CoordChanged(QPointF coord);
    void JuliaPointerChanged(QPointF coord);
    void IterPointsChanged(QVector<QPointF> points);

    void MappingChanged(string mapping);
    void TReChanged(float re);
    void TImChanged(float im);

public slots:

    void EnableJuliaPointer();
    void DisableJuliaPointer();
    void Repaint();
    void Recount(bool now = false);

    void SetTRe(float re, bool need_recount = true);
    void SetTIm(float im, bool need_recount = true);
    void SetMapping(string mapping);

private:

    void CountIterPoints();

    QRgb* pixels_;
    QSize size_;
    QRectF draw_;

    bool mouse_is_pressed_;
    bool rect_is_drawing_;
    QPoint mouse_old_;
    QRectF zoom_rect_;

    bool has_julia_pointer_;
    QPoint julia_pointer_;
    static const int julia_pointer_rad_ = 4;
    bool julia_pointer_move_;

    static const int counters_num_ = 1;
    CountersType counters_;

    QPointF t_;
    string mapping_;
};

//-----------------------------------------------------------------------------

#endif // MANDELBROTPICTURE_H_
